package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentEqpBom;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpBomRecords;
import com.mxpio.erp.common.equipment.service.EquipmentEqpBomRecordsService;
import com.mxpio.erp.common.equipment.service.EquipmentEqpBomService;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 设备BOMService业务层处理
 *
 * @author Cheng
 * @date 2023-07-01
 */
@Service
public class EquipmentEqpBomServiceImpl extends BaseServiceImpl<EquipmentEqpBom> implements EquipmentEqpBomService {
    @Resource
    private EquipmentEqpBomRecordsService eqpBomRecordsService;

    @Override
    @Transactional
    public void change(EquipmentEqpBom equipmentEqpBom) {
        //设置更换记录
        EquipmentEqpBomRecords records = new EquipmentEqpBomRecords();
        records.setBasicsCode(equipmentEqpBom.getBasicsCode());
        records.setBomCode(equipmentEqpBom.getBomCode());
        records.setBomId(equipmentEqpBom.getId());
        records.setBomName(equipmentEqpBom.getBomName());
        records.setBusinessId(equipmentEqpBom.getOrderId());
        records.setBusinessCode(equipmentEqpBom.getOrderCode());
        records.setIsDel(0);
        records.setReplaceDate(new Date());
        records.setReason(equipmentEqpBom.getReasonType());
        records.setOperationType(equipmentEqpBom.getOperateType());
        records.setSpecType(equipmentEqpBom.getSpecType());
        eqpBomRecordsService.save(records);
        //更新BOM信息
        EquipmentEqpBom tpmEqpBom = this.getById(EquipmentEqpBom.class,equipmentEqpBom.getId());
        tpmEqpBom.setBindDate(equipmentEqpBom.getBindDate());
        tpmEqpBom.setLifeCycle(equipmentEqpBom.getLifeCycle());
        tpmEqpBom.setWarnDate(equipmentEqpBom.getWarnDate());
        tpmEqpBom.setPic(equipmentEqpBom.getPic());
        this.update(tpmEqpBom);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<EquipmentEqpBom> listPageWithItem(Pageable page, Criteria criteria) {
        return JpaUtil.linq(EquipmentEqpBom.class).collect("itemCode", Item.class,"bomCode").where(criteria).paging(page);
    }
}
